package com.veo.controller;

import com.veo.exception.UserNotExitException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class MyExceptionHandler {

    //方法1（没有自适应效果）
    //要处理的异常
//    @ResponseBody
//    @ExceptionHandler(UserNotExitException.class)
//    public Map<String,Object> handleException(Exception e){
//        Map<String,Object> map=new HashMap<>();
//        map.put("code","user.notexit");
//        map.put("message","用户不存在哦！");
//        return map;
//
//    }


    @ExceptionHandler(UserNotExitException.class)
    public String handleException(Exception e, HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        //传入状态码
        request.setAttribute("javax.servlet.error.status_code",500);
        map.put("code","user.notexit");
        map.put("message","用户不存在哦！");
        request.setAttribute("exc",map);
        //转发到error
        return "forward:/error";

    }
}
